<?php
/*
 * green controller 2012.8.16 green.php
 */

class green extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ('commonClass');
		session_start();
	}
	
	function index(){
		$id = commonClass::Listion_user ();
		$lang["error"] = "";
		//获取整形ip
		$uip = $this->ip2int($_SESSION['ip']);
		if ($id) {
			$lang['uid'] = $_SESSION['uid'];
			$lang['usermail'] = $_SESSION['usermail'];
			$uid = $_SESSION['uid'];
			//获取会员返利申请列表
			$lang['res'] = $this->m_tbk->get_rebate_rec($uip,$uid);
		}
		else{
			//获取返利申请列表
			$lang['res'] = $this->m_tbk->get_rebate_rec($uip);
		}
		if($lang['res'] == ""){
			$lang['res'] = array();
		}
		$this->load->view('v_green',$lang);
	}
	
	function add_rec(){
		$id = commonClass::Listion_user ();
		if ($id) {
			$data['uid'] = $_SESSION['uid'];
			$data['usermail'] = $_SESSION['usermail'];
		}
		$res['trade_id'] = $this->input->post('trade_id');
		$res['tao_user'] = $this->input->post('alipay');
		$res['submit_date'] = time();
		if(preg_match('/\'\/^\\s*$|^c:\\\\con\\\\con$|[%,\\*\\"\\s\\t\\<\\>\\&\'\\\\]/',$res['trade_id'])){
			$data['error'] = 1;
			$data['msg'] = $this->_lang['add_rebate_error'];
		}
		else{
			if($_SESSION['uid']>0){
				$res['uid'] = $_SESSION['uid'];
			}
			$res['uip'] = $this->ip2int($_SESSION['ip']);
			//添加 trade_id tao_user到返利申请表
			$aa = $this->m_tbk->add_rec($res);
			if($aa){
				$data['error'] = 0;
				$data['tid'] = $res['trade_id'];
				$data['uid'] = $res['tao_user'];
			}
			else{
				$data['error'] = 1;
				$data['msg'] = $this->_lang['add_rebate_error'];
			}
		}
		$this->load->view('v_green',$data);
	}
	//获取整形ip
	private function ip2int($uip) {
		$sIP = $uip;
		$aIP = explode ( '.', $sIP );
		$IP = ($aIP [0] << 24) | ($aIP [1] << 16) | ($aIP [2] << 8) | $aIP [3];
		if ($IP < 0) {
			$IP += 4294967296;
		}
		return $IP;
	}
}